//
//  KXActionSheet.m
//  DD
//
//  Created by HaiJiao Chen on 12-8-23.
//  Copyright (c) 2012年 Kaixin. All rights reserved.
//

#import "KXActionSheet.h"

@implementation KXActionSheet
@synthesize p1;
@synthesize p2;

- (id)init
{
    self = [super init];
    if (self)
    {
        actionSheet = [[UIActionSheet alloc] init];
        actionSheet.delegate = self;
        codes = [[NSMutableDictionary alloc] initWithCapacity:10];
        params = [[NSMutableDictionary alloc] initWithCapacity:10];
    }
    return self;
}

- (id)initWithTitle:(NSString *)title
  cancelButtonTitle:(NSString *)cancelButtonTitle cancelCode:(actionSheetBlock)cancelCode cancelParam:(id)cancelParam
destructiveButtonTitle:(NSString *)destructiveButtonTitle destructiveCode:(actionSheetBlock)destructiveCode destructiveParam:(id)destructiveParam
  otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    self = [super init];
    if (self)
    {
        actionSheet = [[UIActionSheet alloc] init];
        actionSheet.delegate = self;
        codes = [[NSMutableDictionary alloc] initWithCapacity:10];
        params = [[NSMutableDictionary alloc] initWithCapacity:10];
        actionSheet.title = title;
        int count = 0;
        if (destructiveButtonTitle)
        {
            [actionSheet addButtonWithTitle:destructiveButtonTitle];
            if (destructiveParam)
            {
                [params setObject:destructiveParam forKey:[NSNumber numberWithInt:count]];
            }
            if (destructiveCode)
            {
                [codes setObject:[[destructiveCode copy] autorelease] forKey:[NSNumber numberWithInt:count]];
            }
            count++;
        }
        va_list argList;
        if (otherButtonTitles)
        {
            NSString* title;
            va_start(argList, otherButtonTitles);
            while ((title = va_arg(argList, NSString*)))  
            {  
                [actionSheet addButtonWithTitle:title];
                actionSheetBlock code = va_arg(argList, actionSheetBlock);
                [params setObject:code forKey:[NSNumber numberWithInt:count]];
                [params setObject:va_arg(argList, id) forKey:[NSNumber numberWithInt:count]];
                count++;
            }
            va_end(argList);
        }
        if (cancelButtonTitle)
        {
            [actionSheet addButtonWithTitle:cancelButtonTitle];
            if (cancelParam)
            {
                [params setObject:cancelParam forKey:[NSNumber numberWithInt:count]];
            }
            if (cancelCode)
            {
                [codes setObject:[[cancelCode copy] autorelease] forKey:[NSNumber numberWithInt:count]];
            }
            count++;
        }
        
        
        if (destructiveButtonTitle)
        {
            actionSheet.destructiveButtonIndex = 0;
        }
        if (cancelButtonTitle)
        {
            actionSheet.cancelButtonIndex = count-1;
        }
    }
    return self;
}

-(NSInteger)cancelButtonIndex
{
    return actionSheet.cancelButtonIndex;
}
-(void)setCancelButtonIndex:(NSInteger)cancelButtonIndex
{
    actionSheet.cancelButtonIndex = cancelButtonIndex;
}

- (NSInteger)destructiveButtonIndex
{
    return actionSheet.destructiveButtonIndex;
}
-(void)setDestructiveButtonIndex:(NSInteger)destructiveButtonIndex
{
    actionSheet.destructiveButtonIndex = destructiveButtonIndex;
}

-(NSString*)title
{
    return actionSheet.title;
}
-(void)setTitle:(NSString *)title
{
    actionSheet.title = title;
}


- (NSInteger)addButtonWithTitle:(NSString *)title code:(actionSheetBlock)code param:(id)param;
{
    NSInteger index = [actionSheet addButtonWithTitle:title];
    if (code)
    {
        [codes setObject:[[code copy] autorelease] forKey:[NSNumber numberWithInt:index]];
    }
    if (param)
    {
        [params setObject:param forKey:[NSNumber numberWithInt:index]];
    }
    return index;
}
- (void)showInView:(UIView *)view
{
    [self retain];
    [actionSheet showInView:view];
}

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
    actionSheet.delegate = nil;
    [actionSheet dismissWithClickedButtonIndex:-1 animated:animated];
    [self release];
}

-(void)dealloc
{
    [params release];
    [codes release];
    [actionSheet release];
    [p1 release];
    [p2 release];
    [super dealloc];
}



- (void)actionSheetCancel:(UIActionSheet *)_actionSheet
{
    actionSheet.delegate = nil;
    [self release];
}

- (void)actionSheet:(UIActionSheet *)_actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    actionSheet.delegate = nil;
    actionSheetBlock p = [codes objectForKey:[NSNumber numberWithInt:buttonIndex]];
    if (p)
    {
        p(self.p1, self.p2, [params objectForKey:[NSNumber numberWithInt:buttonIndex]]);
    }
    [self release];
}

@end
